<?php
/* TODO: Add code here */
class ArticleModel extends Model{
	function init(){
		}
		
	function getArticleList($row=null,$type=null,$sort='asc'){
		$this->OpenDB();
		$sql="SELECT * FROM `@#__article_list` ";
		if($type)$sql.="where article_type=$type ";
		$sql.="order by add_time $sort ";
		if($row)$sql.="limit 0,$row";
		$rs=$this->db->fetchRows($sql);
		return $rs;
		}
		
	function getList($type=null,$page=null,$limit=null,$sort='desc'){
		$sql="SELECT * FROM `@#__article_list`";
		if($type)$sql.=" WHERE article_id=$id";
		$sql.="order by add_time $sort ";
		$this->OpenDB();
		$count=$this->db->num_rows($this->db->query($sql));
		if($limit){
			if ($page){
				if($page>ceil($count/$limit))
				$page=$count/$limit;
				elseif ($page<1) {
					$page=1;
				}
				$start=$page*$limit-$limit;
				if ($start<0)$start=0;
			} 
			else 
			$start=0;
			$sql.=" limit $start,$limit";
		} 
		$rt['data']=$this->db->fetchRows($sql,true);
		$rt['count']=$count;
		@$rt['pagecount']=ceil($count/$limit);
		return $rt;
	}
	
	 function getView($id){
		$this->OpenDB();
		$sql="SELECT * FROM `@#__article_list` WHERE article_id=$id";
		$rs=$this->db->get_one($sql);
		return $rs;
	}
	
	function setReadCount($id){
		$sql="UPDATE `@#__article_list` SET article_read=article_read+1 WHERE article_id=$id";
		$this->OpenDB();
		$this->db->query($sql);
	}
	
	function getRe($id){
		$this->OpenDB();
		$sql="SELECT * FROM @#__article_re WHERE article_id=$id order by article_re_id asc";
		$count=$this->db->num_rows($this->db->query($sql));
		$rt['data']=$this->db->fetchRows($sql,true);
		$rt['count']=$count;
		return $rt;
	}
	/**
	 * 回复
	 * Enter description here ...
	 * @param unknown_type $articleid
	 * @param unknown_type $content
	 * @param unknown_type $reuser
	 * @param unknown_type $reuserid
	 */
	function ReArticle($articleid,$content,$reuser,$reuserid){
	
		$this->OpenDB();
		$query=$this->db->get_one("select * from @#__article_list WHERE article_id=$articleid");
		if(!$query)return '回复的文章不存在！';
		$sql="INSERT INTO `@#__article_re`(article_id,re_content,re_user,re_nick,re_time)
		VALUES($articleid,'".addslashes(htmlspecialchars($content))."',$reuserid,'$reuser',unix_timestamp())";
		$this->db->query($sql);
		return '回复成功';
	}
	
	function UpdateArticle($title,$content,$id){
		$sql="UPDATE `@#__article_list` SET article_title='$title',article_text='$content',edit_time=unix_timestamp() WHERE article_id=$id";
		$this->OpenDB();
		return $rs=$this->db->query($sql);
	}
	
	function SaveArticle($title,$content,$userid){
		$sql="INSERT INTO `@#__article_list`(article_title,article_text,article_user,add_time,edit_time)
		VALUES('$title','$content',$userid,unix_timestamp(),unix_timestamp())";
		$this->OpenDB();
		return $rs=$this->db->query($sql);
	}
	
	function DelArticle($id){
		$sql="DELETE FROM `@#__article_list` WHERE article_id=$id";
		$this->OpenDB();
		return $this->db->query($sql);
	}
}
?>